const app = getApp();

var data = {
  xhx_images: app.xhx_images,
  xhx_route: app.xhx_route,
  countDown: 60,
  isClickGetCode: false, //是否点击过获取验证码
  xform_gymName: "", //健身房名称
  xform_type: "", //健身房分类
  xform_province: "", //省
  xform_city: "", //市
  xform_area: "", //区
  xform_address: "", //健身房地址
  xform_username: "", //负责人姓名
  xform_phone: "", //手机号
  xform_code: "", //手机号验证码
  xform_longitude: 9999, //经度
  xform_latitude: 9999, //维度
  xform_license: "", //营业执照
  xform_isAgree: "", //同意协议
  reg: {
    xform_gymName: '健身房名称不为空',
    xform_type: '健身房类型不为空',
    xform_province: '地区不为空',
    xform_address: '详细地址不为空',
    xform_username: '负责人不为空',
    xform_phone: '手机号不为空',
    xform_code: '验证码不为空',
    xform_isAgree: '请同意协议',
  },

  dialogValue:null,  //弹框传值
};

var currentPage = {
  data,
  typeParams: {}, //获取筛选标签参数
  onLoad,
  getSearchLabelByType, //获取筛选标签
  navigationBack,
  changeType,
  chooseLocation,
  changePhone, //手机号
  getCode, //获取验证码
  sendPhoneCode, //获取验证码请求
  checkForm,
  setFormData,
  formSubmit,
  changeAgree,
};

var page = Object.assign({}, currentPage);

Page(page);

function onLoad(options) {
  const that = this;
  const json = options.json;
  if(json){
    that.setData(JSON.parse(json))
  }
  that.getSearchLabelByType();
}

function getSearchLabelByType() {
  const that = this;
  that.typeParams.type = 2;
  app.middleware.requestAjax({
    serAddr: app.serAddrs.getSearchLabelByType,
    data: that.typeParams,
    cancelToast: true,
    success: function(res) {
      let types = res.data;
      let temp = {
        types
      };
      that.setData(temp);
    },
    fail: function(res) {

    }
  })
}

function navigationBack() {
  // wx.showModal({
  //   title: '提示',
  //   content: '离开此页面，入驻信息将会丢失',
  //   success: function(res) {
  //     if (res.confirm) {
  //       wx.navigateBack();
  //     }
  //   }
  // })
  var dialogValue={
    showDialog:true,
    title:'提示',
    content:'离开此页面，入驻信息将会丢失',
    confirmText: '确定',
    cancelText:'取消',
    confirm: function(res) { 
      wx.navigateBack();
    },
    cancel:function(res){
      
    }
  }
  this.setData({
    dialogValue:dialogValue
  })
}

function changeType(event) {
  const that = this;
  const index = event.detail.value;
  const types = that.data.types;
  that.setData({
    "xform_type": types[index].id,
  })
}

function chooseLocation() {
  const that = this;
  let form = that.data.form;
  wx.chooseLocation({
    success: function(res) {
      const latitude = res.latitude;
      const longitude = res.longitude;
      app.qqmapsdk.reverseGeocoder({
        location: {
          latitude,
          longitude,
        },
        success: function(res) { //成功后的回调
          const address_component = res.result.address_component || {};
          const province = address_component.province;
          const city = address_component.city;
          const area = address_component.district;
          that.setData({
            "xform_province": province,
            "xform_city": city,
            "xform_area": area,
            "xform_latitude": latitude,
            "xform_longitude": longitude,
          })
        }
      });
    },
  })
}

function changePhone(event) {
  const that = this;
  const phone = event.detail.value;
  that.setData({
    "xform_phone": phone,
  })
}

function getCode() {
  const that = this;
  let phone = that.data["xform_phone"];
  phone = phone.replace(/[^0-9]/ig, "");
  if (app.middleware.isPoneAvailable(phone)) {

    that.sendPhoneCode();
    that.setData({
      isClickGetCode: true,
      countDown: that.data.countDown - 1,
    })
    var timmer = setInterval(() => {
      that.setData({
        countDown: that.data.countDown - 1,
      })
      if (that.data.countDown == 0) {
        clearInterval(timmer);
        that.setData({
          countDown: 60,
        })
        return false;
      }
    }, 1000)
  } else {
    app.middleware.showToast("手机号错误");
  }
}

function sendPhoneCode() {
  const that = this;
  const phone = that.data["xform_phone"];
  var reqData = {
    phone,
  }
  app.middleware.requestAjax({
    serAddr: app.serAddrs.sendPhoneCode1,
    data: reqData,
    cancelToast: true,
    success: function(res) {

    }
  })
}

function formSubmit(e) {
  const that = this;
  const form = that.data;
  let temp = {};
  that.setFormData(e.detail.value);
  if (this.checkForm()) {

    for (let key in form) {
      let arr = key.split("xform_");
      if (arr.length == 2) {
        temp[arr[1]] = form[key];
      }
    }

    app.middleware.requestAjax({
      serAddr: app.serAddrs.addOrUpdate,
      data: temp,
      success: function (res) {
        wx.redirectTo({
          url: app.xhx_route.applyDetail,
        })
      },
      fail: function (res) {
        app.middleware.showToast(res && res.msg || "请求失败");
      }
    })

    // wx.redirectTo({
    //   url: `${app.xhx_route.applyUpLoad}?json=`+JSON.stringify(temp),
    // })
  }
}

function changeAgree() {
  const that = this;
  let temp = {};
  let xform_isAgree = that.data.xform_isAgree;
  temp.xform_isAgree = xform_isAgree == 1 ? 0 : 1;
  that.setData(temp);
}

function setFormData(form) {
  const that = this;
  let temp = {};
  for (var key in form) {
    temp["xform_" + key] = form[key];
  }
  that.setData(temp);
}

function checkForm() {
  var that = this;
  const reg = that.data.reg;
  for (var key in reg) {
    console.log(key)
    if (!that.data[key]) {
      wx.showToast({
        title: that.data.reg[key],
        icon: 'none',
        duration: 2000
      })
      return false;
    }
  }
  return true;
}